#!/usr/bin/python

# Copyright (c) 2012, United States Government, as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All rights reserved.
#
# The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

""" Splits an imported file according to input """
# Purpose: Converts a multi-source scores.csv into a single source file
# Author:  Brian Mirletz, Alexander Xydes
# Date:    January 2015
# Notes:   Converts a scores.csv file into csv files that
# would have been generated by a single evolution object Input parameters are:
# (1) The name of the scores file
# (2) The number of evolution objects used to generate the scores file

import sys
import csv
from operator import itemgetter
from itertools import islice
import numpy as np

def splitInfile(inFile, numSplit):

    for x in range(0, int(numSplit)) :
        try:
            fi = open(inFile, 'r')
            inFilePart = inFile.partition('.')
            outFile = inFilePart[0] + '_' + str(x) + inFilePart[1] + inFilePart[2]
            fo = open(outFile, 'w')
            fo.writelines(islice(fi, 0 + x, None, int(numSplit)))
        finally:
            fi.close()
            fo.close()



if __name__=="__main__":
    inFile = sys.argv[1]
    numSplit = sys.argv[2]
    splitInfile(inFile, numSplit)

